package com.ukao.steward.ui.login;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.VerifysBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.listener.MyTextWatcher;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.T;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 验证登录
 */
public class VerifyLoginFrament extends BaseFragment {


    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.signin_btn)
    Button signinBtn;
    @BindView(R.id.msg)
    TextView msg;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private Unbinder unbinder;

    public static VerifyLoginFrament newInstance(String param1, String param2) {
        VerifyLoginFrament fragment = new VerifyLoginFrament();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_verify_login, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initListener();
        return view;
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        setStatusBarTintColor(R.color.white);
    }

    @Override
    protected void initView() {
        signinBtn.setEnabled(false);
        signinBtn.setAlpha(0.5f);
    }


    @Override
    protected void initListener() {
        super.initListener();
        phone.addTextChangedListener(useNameTextWatcher);


    }

    private MyTextWatcher useNameTextWatcher = new MyTextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            super.onTextChanged(s, start, before, count);
            isILoginGo(s);
        }
    };

    private void isILoginGo(CharSequence s) {
        String EditText = s.toString().trim();

        if (EditText.isEmpty()) {
            if (signinBtn.isEnabled()){
                signinBtn.setAlpha(0.5f);
                signinBtn.setEnabled(false);
            }

        } else  if (!signinBtn.isEnabled()){
            signinBtn.setAlpha(1f);
            signinBtn.setEnabled(true);
        }
    }

    @OnClick({R.id.phone, R.id.signin_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.phone:
                break;
            case R.id.signin_btn:
                String strPhone = getText(phone);
                if (CheckUtils.isEmpty(strPhone)) {
                    T.show("账号不能为空");
                    return;
                }
                SaveParams.setSerialcount(strPhone);
                verifyAccount(strPhone);
                break;
        }
    }


    public void verifyAccount(final String strPhone) {
        showProgressDialog();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("account", strPhone);
        addSubscription(apiStores().verifyAccount(Constant.createParameter(mHashMap)),
                new ApiCallback<VerifysBean>() {
                    @Override
                    public void onSuccess(VerifysBean model) {

                        if (model.getHttpCode() == 200) {

                            VerifysBean.DataBean mVerifysBean = model.getData();
                            mVerifysBean.setAccount(strPhone);
                            start(SetNewPassFrament.newInstance(mVerifysBean,""));


                        } else {

                            T.show(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                        dismissProgressDialog();

                    }
                });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
